﻿
@{
    ViewBag.Title = "IV本地数据格式配置";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkShop}}：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                <option value="">全部车间</option>
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        var ivSqls = new Array();

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer'], function () {
                var layer = layui.layer,
                    table = layui.table;

                var options = {
                    elem: '#table'
                    , url: '/ParaSetting/QueryIVLocalDatas'
                    , method: "post"
                    , where: { workshopCode: '' }
                    , parseData: function (res) {
                        ivSqls = res.Data;
                        return res;
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60 }
                        , { field: 'WorkshopName', title: '车间' }
                        , { field: 'LineName', title: '产线' }
                        , { field: 'MachineName', title: '设备名称' }
                        , { field: 'Manufacturers', title: '厂家' }
                        , { field: 'MachineModel', title: '型号' }
                        , { field: 'Sql', title: 'SQL' }
                        , {
                            title: '操作', width: 200, templet: function (d) {
                                return '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="edit(' + d.MachineId + ')"><i class="layui-icon">&#xe642;</i>编辑</button>';
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.where.workshopCode = $("#selWorkshops").val().trim();
                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function edit(machineId) {
            var ivSql = ivSqls.find(function (e) {
                return e.MachineId == machineId;
            });
            layer.prompt({
                formType: 2,
                value: ivSql.Sql,
                title: '请输入SQL语句',
                area: ['800px', '350px']
            }, function (value, index, elem) {
                saveSql(machineId, value);
                layer.close(index);
            });
        }

        function saveSql(machineId, sql) {
            var loadId = layer.load();
            $.ajax({
                url: "/ParaSetting/EditIVLocalDataMgr",
                data: {
                    model: {
                        MachineId: machineId,
                        Sql: sql
                    }
                },
                complete: function () {
                    layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        layer.msg(res.ErrMsg, { icon: 5 });
                        return;
                    }
                    $("#btnSearch").click();
                }
            });
        }
    </script>
}
